<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>设备详情</title>
<meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<script type="text/javascript">

//获取XmlHttpRequest对象
function createXmlHttpRequest() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
}

//获取url后的参数值
function GetRequest() {
	   var url = location.search; //获取url中"?"符后的字串
	   var theRequest = new Object();
	  
	   if (url.indexOf("?") != -1) {
	      var str = url.substr(1);
	      strs = str.split("&");
	      for(var i = 0; i < strs.length; i ++) {
	         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
	      }
	      
	   }
	   return theRequest;
}

//获取url路径#后的参数值
function GetParam(){
	
	var Url=top.window.location.href;
	var u='',g='';
	if(Url.indexOf("#")){
		u=Url.split("#");
	}else if(Url.indexOf("?")){
		u=Url.split("?");
	}
	if (u.length==1){
		g='';
	}else{
		g=u[1]; 
	}
	
	   return g;
}
//获取项目名
var pathName=window.document.location.pathname;
var ctx=pathName.substring(0,pathName.substr(1).indexOf('/')+1);

function GetUrl(){
	 var pathName=window.document.location.pathname;
	 var projectName=pathName.substring(pathName.substr(1).indexOf('/')+1);
	var url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx31cf436485b2fdf8&redirect_uri=http%3a%2f%2f203.195.189.85%2flifesense-healthcenter%2fwechat%2fauthx4HTML%3ft%3d"+projectName+"&response_type=code&scope=snsapi_base&state=gh_4bc17495d97a#wechat_redirect";

	return url;
}

//
function GetRequestValue(){
//	var param=new Object();
//	param = GetRequest();
	
	var xhr = createXmlHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
					jsonData = eval( "(" + json + ")" );//转换后的JSON对象
					if(jsonData.accountId != null){
						GetMemberId(jsonData.accountId);
					}
			}
		}
	};
	var url = ctx+"/wechat/ajax_webAuthorize";
//	xhr.open("post", url + "?code=" + param["code"] +"&&state="+param["state"]+"&&scope="+param["scope"]+"&&mathNum="+Math.random(), true);
	xhr.open("post", url+"?num=1", true);
	xhr.send();
}

function GetMemberId(accountId){
	var xhr = createXmlHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
				document.getElementById("memberId").value=json;
				//初始化数据	
				ajaxLoadData();
			}
		}
	};
	var url = ctx+"/wechat/ajax_openId";
	xhr.open("post", url + "?accountId="+accountId+"&&mathNum="+Math.random(), true);
	xhr.send();
}

//显示块
function showDOM(id){
	var dom = document.getElementById(id);
	dom && (dom.style.display = 'block');
}
//隐藏块
function hiddenDOM(id){
	var dom = document.getElementById(id);
	dom && (dom.style.display = 'none');
}

	var readyFunc = function onBridgeReady() {
		WeixinJSBridge.on('onWXDeviceBindStateChange', function(argv) {
			// console.log(argv.state);å
			// alert(argv.state);
			var deviceId = document.getElementById("hidden_deviceId").value;
			if (argv.state == "bind") {
				connectWXD(deviceId);
			} else {
				disconnectWXD(deviceId);
			}
		});
		WeixinJSBridge.on('onWXDeviceStateChange', function(argv) {
			// console.log(argv.state);
			// alert(argv.state);
		});
		WeixinJSBridge.on('onScanWXDeviceResult', function(argv) {
			// console.log(argv);
			// alert(searchOrGet);
		});
		WeixinJSBridge.on('onReceiveDataFromWXDevice', function(argv) {
			console.log(argv);
		});
		//等新包下来再测试
		WeixinJSBridge.on('onWXDeviceBluetoothStateChange', function(argv) {
			console.log(argv);
			// alert("blueState:" + argv.state);
			if (argv.state == "off") {
				//蓝牙关闭显示提示框
				alertWin('alert_blue');
			} else {
				//蓝牙开启关闭提示框
				removeMybg('alert_blue');
			}
		});
	};
	if (typeof WeixinJSBridge === "undefined") {
		document.addEventListener('WeixinJSBridgeReady', readyFunc, false);
	} else {
		readyFunc();
	}
</script>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
}

body {
	font-family: Helvetica;
	background-color: #f1f3f4;
}

.all {
	width: 100%;
	color: #4c4d4f;
	background-color: #f1f3f4;
}

.device_div {
	width: 100%;
	height: 70px;
	border-bottom: solid 1px #e8e8e8;
	line-height: 70px;
	color: #3E3D3E;
	background-color: #fff;
}

.device_img {
	width: 42px;
	height: 42px;
	float: left;
	margin-top: 15px;
	margin-left: 13px;
}

.device_text {
	float: left;
	margin-left: 14px;
	margin-top: 1px;
}

.deviceMemberCount_div{
	float: right;
	line-height: 70px;
	display:none; 
	font-size: 13px;
  	color: #a3a2a3;
  	padding-top: 1px;
}

.device_right {
	float: right;
	margin-top: 16px;
	margin-right: 8px;
	width: 40px;
}

.bottom {
	width: 100%;
	height: 150px;
	background-color: #f1f3f4;
	border-top: 1px solid #f1f3f4;
}

.deleteDevice {
	background-color: #ec8431;
	width: 85px;
	line-height: 30px;
	height: 30px;
	text-align: center;
	color: #fff;
	border-radius: 30px;
	margin: 45px auto 0;
}

.device_detail {
	width: 100%;
	border-bottom: 1px solid #e8e8e8;
	height: 150px;
	background-color: #fff;
}

.device_detail_img {
	width: 90px;
	/* height: 70px; */
	float: left;
	margin-top: 40px;
	margin-left: 16px;
}

.device_detail_right {
	float: left;
}

.device_detail_right1 {
	margin-top: 45px;
	margin-left: 10px;
}

.device_detail_text {
	float: left;
	font-size: 17px;
	margin-left: 16px;

	/* overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 50%; */
}

.device_detail_edit {
	float: left;
	width: 32px;
	margin-top: -7px;
	margin-left: 7px;
}

.device_detail_right2 {
	float: left;
	margin-top: 15px;
}

.power {
	color: #ec8431;
	float: left;
	margin-left: 2px;
	margin-top: 1px;
}

.device_detail_power {
	float: left;
	width: 23px;
	margin-left: 27px;
}

.alert_Exit {
	width: 270px;
	height: 215px;
	border-radius: 5px;
	background: #fff;
	border: 1px solid #ccc;
	position: absolute;
	z-index: 3000;
	display: none;
	left: 50%;
	top: 50%;
	margin-left: -135px;
	margin-top: -85px;
	-webkit-border-radius: 8px;
	-moz-border-radius: 8px;
	border-radius: 8px;
	-webkit-box-shadow: #666 0px 0px 20px;
	-moz-box-shadow: #666 0px 0px 20px;
	box-shadow: #666 0px 0px 20px;
}

.alert_Exit .alert_Exit_title {
	width: 100%;
	height: 50px;
	line-height: 50px;
	font-size: 16px;
	text-align: center;
	/* margin-top: 15px; */
	margin-top: 13px;
	color: #f7872c;
}

.alert_Exit .alert_text {
	float: left;
	text-align: center;
	margin-left: 4px;
}

.alert_Exit .alert_img {
	float: left;
	margin-top: 15px;
	margin-left: 69px;
}

.alert_Exit .alert_Exit_tips {
	width: 100%;
	height: 40px;
	line-height: 25px;
	font-size: 15px;
	/* font-weight:900; */
	color: #848484;
	margin-top: 8px;
	/* padding-left:10px; */
	/* padding-right:10px; */
}

.alert_outExit_tips {
	height: 50px;
	line-height: 20px;
	font-size: 15px;
	font-weight: 900;
	color: #848484;
	margin-left: 30px;
	margin-right: 30px;
}

.alert_Exit_contentDiv3 {
	height: 30px;
	width: 80%;
	margin: auto;
}

.alert_Exit .alert_Exit_cancel {
	float: left;
	width: 25%;
	text-align: center;
	margin: 37px 0 10px 30px;
}

.alert_Exit .alert_Exit_ok {
	margin: 37px 30px 10px 0;
	float: right;
	width: 25%;
	text-align: center;
}

.alert_Exit .alert_edit_title {
	width: 100%;
	height: 50px;
	line-height: 50px;
	font-size: 16px;
	text-align: center;
	margin-top: 13px;
	color: #f7872c;
}

.device_name {
	border: solid 1px #e8e8e8;
	height: 40px;
	/* line-height: 40px; */
	width: 100%;
	/* margin-top: 5px; */
}

input[type="text"]:focus {
	outline: none;
	-webkit-box-shadow: none;
	box-sizing: border-box;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	-webkit-appearance: none;
}

.alert_Exit input[type="text"] {
	width: 100%;
	font-size: 15px;
	margin: 0;
	display: block;
	padding: 0;
	box-sizing: border-box;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	-webkit-appearance: none;
	cursor: text;
	outline: none;
	-webkit-box-shadow: none;
}

.alert_edit_tips {
	width: 100%;
	height: 40px;
	font-size: 15px;
	color: #848484;
	margin-top: 7px;
}

.alert_edit_contentDiv3 {
	width: 80%;
	margin: auto;
}

.prompt {
	font-size: 12px;
	margin-top: 6px;
	margin-left: 10%;
	font-weight: bold;
}

.popt {
	height: 15px;
	line-height: 15px;
	text-align: center;
	margin-top: 50%;
	color: #3E3D3E;
	display: none;
}

#alert_bottom {
	margin-top: 5px;
}

/**弹出框按钮*/
.alert_win_btn{
	width: 35px;
	height: 35px;
	border-radius: 35px;
	font-size: 14px;
	line-height: 35px;
	font-weight: bold;
	margin: auto;
	text-align: center;
}
/**弹出确认按钮*/
.alert_win_ok_btn{
	color:#f7872c;
	border:solid 1px #f7872c;
}
/**弹出取消按钮*/
.alert_win_cancel_btn{
	color:#848484;
	border:solid 1px #848484;
}
</style>
</head>

<body id="b" style="display: none">
	<input type="hidden" id="hidden-appId" value="wx0e54f358b6eb5ce0" />
	<input type="hidden" id="hidden-timestamp" value="1436324132" />
	<input type="hidden" id="hidden-nonceStr" value="iZlusA6m0XUwcRFS" />
	<input type="hidden" id="hidden-signature" value="7a067fb548396d3687311c254a9c00546e3e4f9c" />

	<input type="hidden" id="deviceId" value="ca040507d6f1"/>
	<input type="hidden" id="openId" value="ouz52jiY7gfJd3vK-9K6T9mBRPuo"/>
	<input type="hidden" id="memberId" value="00bd2e9abb4c46a298eb6b2962dbaf3a"/>
	<input type="hidden" id="carrier" value=""/>
	
	<input type="hidden" id="deviceType"/>

	<div class="all" id="all">


		<div class="device_detail">
			<img class="device_detail_img" id="device_detail_img"
				src="/weixin/v1/img/device/bonbon.png" />

			<div class="device_detail_right">
				<div class="device_detail_right1" id="device_detail_right1">
					<div class="device_detail_text" id="device_detail_text"></div>
					<img alt="" onclick="deleteDevice('alert_edit');"
						class="device_detail_edit" src="/weixin/v1/img/device/pen.png" />
				</div>

				<div class="device_detail_right2" id="device_detail_right2">
					<img alt="" class="device_detail_power"
						src="/weixin/v1/img/device/power.png" />
					<div class="power">
						电量<font id="power">100%</font>
					</div>
				</div>


			</div>


		</div>



		<div class="device_div" id="device_div_user" style="display: none"
			onclick="showBinding()">
			<img class="device_img" src="/weixin/v1/img/device/set_user.png" />
			<div class="device_text">用户</div>
			<img class="device_right" alt=""
				src="/weixin/v1/img/setting_arrow.png"/>
			<div class="deviceMemberCount_div" id="deviceMemberCount_div"></div>
		</div>

		<div class="device_div" onclick="showQRCode()">
			<img class="device_img" src="/weixin/v1/img/setting_qr1.png" />
			<div class="device_text">二维码</div>
			<img class="device_right" alt=""
				src="/weixin/v1/img/setting_arrow.png"/>
		</div>
		
		<div id="wifi_setting" style="display:none;" class="device_div" onclick="showWIFISetting()">
			<img class="device_img" src="/weixin/v1/img/wifi_setting.png" />
			<div class="device_text">配置Wi-Fi密码</div>

			<img class="device_right" alt=""
				src="/weixin/v1/img/setting_arrow.png"/>
		</div>

		<div class="device_div" onclick="zhinan()">
			<img class="device_img" src="/weixin/v1/img/device/zn.png" />
			<div class="device_text">操作指南</div>

			<img class="device_right" alt=""
				src="/weixin/v1/img/setting_arrow.png"/>
		</div>
	</div>
	
	<div id="jsf_button" style="width:100%;height: 70px;line-height: 70px;margin-top: 15px;background: #fff;display:none;">
		<div style="margin-left: 23px;color:#3E3D3E;float: left;">连接上该设备时通知我</div>
		<img id="changeButton" style="float: right;margin-top: 20px;margin-right: 16px;" alt="" ontouchstart="clickLockImg()" src="/weixin/v1/img/device/device_unlock.png" width="45"/>
	</div>
	
	<!-- 隐藏删除按钮 后面要打开 -->
	<div class="bottom" id="bottom" style="display: none;">
		<div class="deleteDevice" id="deleteDevice"
			onclick="deleteDevice('alert_del');">删除设备</div>
	</div>

	<div class="popt" id="popt">当前没有绑定的设备。</div>


	<!-- /删除的弹出框 -->
	<div id="alert_del" class="alert_Exit">
		<div class="alert_Exit_title">
			<img class="alert_img" alt="" src="/weixin/v1/img/match_warning.png"
				width="20" /> <span class="alert_text">确定删除设备</span>
		</div>
		<div class="alert_Exit_tips">
			<div class="alert_Exit_contentDiv3">

				<span>删除<span id="deviceName_del"></span>设备，用户档案记录不会被删除。
				</span>
			</div>
		</div>

		<div style="height: 40px;">
			<div class="alert_Exit_cancel" id="alert_Exit_cancel"
				onclick="alertExitCancel('alert_del');">
				<div id="alert_Exit_close"
					class="alert_win_btn alert_win_cancel_btn">取消</div>
			</div>
			<div class="alert_Exit_ok" id="alert_Exit_ok"
				onclick="alertExitOk('alert_del');">
				<div id="alert_Exit_save" class="alert_win_btn alert_win_ok_btn">确定</div>
			</div>
		</div>
	</div>
	<!-- /删除的弹出框 -->

	<!-- /修改设备名称的弹出框 -->
	<div id="alert_edit" class="alert_Exit">
		<div class="alert_edit_title">
			<span class="alert_edit_text">修改设备名称：</span>
		</div>
		<div class="alert_edit_tips">
			<div class="alert_edit_contentDiv3">
				<input type="text" class="device_name" maxlength="14"
					id="deviceName" />
			</div>

			<div class="prompt" id="prompt"></div>
		</div>

		<div style="height: 40px;" id="alert_bottom">
			<div class="alert_Exit_cancel"
				onclick="alertExitCancel('alert_edit');">
				<div id="alert_edit_close"
					class="alert_win_btn alert_win_cancel_btn">取消</div>
			</div>
			<div class="alert_Exit_ok" onclick="alertExitOk('alert_edit');">
				<div id="alert_edit_save" class="alert_win_btn alert_win_ok_btn">确定</div>
			</div>
		</div>
	</div>
	<!-- /修改设备名称的弹出框 -->

	<!-- 蓝牙未开启提示框 -->
	<div id="alert_blue" class="alert_Exit">
		<div class="alert_Exit_title">
			<img class="alert_img" alt="" src="/weixin/v1/img/match_warning.png"
				width="20" /> <span class="alert_text">蓝牙未开启</span>
		</div>
		<div class="alert_Exit_tips">
			<div class="alert_Exit_contentDiv3">
				<span>手机未开启蓝牙，无法连接设备。请在手机设置中开启蓝牙。</span>
			</div>
		</div>

		<div style="height: 40px;">
			<div class="alert_Exit_ok" ontouchstart="removeMybg('alert_blue');"
				style="width: 100%; text-align: center; float: none;">
				<div id="alert_Exit_save" class="alert_win_btn alert_win_ok_btn">确定</div>
			</div>
		</div>
	</div>
	
</body>

<script type="text/javascript">
/* document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
	WeixinJSBridge.call('hideOptionMenu');
	}); */
	var ctx = "/lifesense-healthcenter";
	function hideWXOptionMenu(){
		try{
			wx.ready(function () {
				wx.hideOptionMenu();
			});
		}catch(err){
			
		}
	}
	window.onload = function() {
		window.addEventListener("pageshow", function() {
			document.title = "设备详情";
			setTimeout(function() {
			}, 100);
		}, false);
		window.addEventListener("pagehide", function() {
			document.title = "";
			setTimeout(function() {
			}, 100);
		}, false);
		//document.getElementById("b").style.display = "none";
		//init_style();
		var param = GetRequest();
		document.getElementById("deviceId").value = param["deviceId"];
		document.getElementById("openId").value = param["openId"];
		document.getElementById("memberId").value = param["memberId"];
		ajaxDeviceDetail();
	}

	function showUI(data) {
		var deviceType = document.getElementById("deviceType").value;
		var device_div_user = document.getElementById("device_div_user");
		if (deviceType == 2) {//体重秤不显示电量
			
			var deviceMemberCount_div=document.getElementById("deviceMemberCount_div");
			deviceMemberCount_div.style.display="block";
			deviceMemberCount_div.innerHTML=data.deviceMemberCount+"人";
			console.log(data)
			
			document.getElementById("device_detail_right2").style.display = "none";
			document.getElementById("device_detail_right1").style.marginTop = "70px";
			device_div_user.style.display = "block";
		} else if(deviceType==1){
			device_div_user.style.display = "none";
			
		}
		
		if(isjsf==2){//健身房体重秤不显示用户
			device_div_user.style.display = "none";
			//document.getElementById("jsf_button").style.display = "block";
		}
		
		/* document.getElementById("jsf_button").style.display = "block"; */
	}

	var jsondata = null;
	function ajaxDeviceDetail() {
		var _openId = document.getElementById("openId").value;
		var _memberId = document.getElementById("memberId").value;
		var deviceId = document.getElementById("deviceId").value;
		var times = new Date().getTime();
		var xhr = createXmlHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var json = xhr.responseText;
					var jsonData = eval("(" + json + ")");
					document.getElementById("b").style.display = "block";
					if (jsonData != null && jsonData != ""
							&& jsonData.device.id) {
						initdata(jsonData);
						jsondata = jsonData;
						if (jsonData.state == 1) {
							document.getElementById("all").style.display = "none";
							document.getElementById("bottom").style.display = "none";
							document.getElementById("popt").style.display = "block";
							document.getElementById("popt").innerHTML = "对不起，您暂时无法查看该页面。";
						}
					} else {
						document.getElementById("all").style.display = "none";
						document.getElementById("bottom").style.display = "none";
						document.getElementById("popt").style.display = "block";
					}
				}
			}
		};
		var url = "/api/ajax/getDeviceDetail";
		xhr.open("post", url + "?openId=" + _openId + "&&memberId=" + _memberId
				+ "&&deviceId=" + deviceId + "&&times=" + times, true);
		xhr.send();
	}
	var isjsf=1;
	
	var carrier = "";
	function initdata(json) {
		var data = json.device;
		carrier = data.carrier;
		document.getElementById("deviceId").value = data.id;
		var name = data.deviceName;
		if (name.length > 0) {
			name = cutstr(name, 14);
		}
		document.getElementById("device_detail_text").innerHTML = name;
		document.getElementById("power").innerHTML = json.batteryPercent + "%";
		//1手环 2体重秤
		var deviceImg = "bonbon.png";
		var modeNum = data.modelNum;
		if (modeNum.toLowerCase().indexOf("405", 0) >= 0) {
			deviceImg = "manbo.png";
		} else if (modeNum.toLowerCase().indexOf("407", 0) >= 0) {
			deviceImg = "bonbon.png";
		} else if (modeNum.toLowerCase().indexOf("408", 0) >= 0) {
			deviceImg = "kband.png";
		} else if (modeNum.toLowerCase().indexOf("410", 0) >= 0) {
			deviceImg = "bonbon_c.png";
		} else if (modeNum.toLowerCase().indexOf("208", 0) >= 0) {
			deviceImg = "LS208_B.png";
		} else if (modeNum.toLowerCase().indexOf("207", 0) >= 0) {
			deviceImg = "LS207-B.png";
		} else if (modeNum.toLowerCase().indexOf("206", 0) >= 0) {
			deviceImg = "LS206-B.png";
		} else if (modeNum.toLowerCase().indexOf("202", 0) >= 0) {
			deviceImg = "ls202-B.png";
		} else if (modeNum.toLowerCase().indexOf("108", 0) >= 0) {
			deviceImg = "s1.png";
		} else if (modeNum.toLowerCase().indexOf("106", 0) >= 0) {
			deviceImg = "A3.png";
		}
		document.getElementById("device_detail_img").src = "/weixin/v1/img/device/" + deviceImg;
		//根据设备类型显示UI
		document.getElementById("deviceType").value = json.deviceType;
		isjsf=json.isjsf;
		showUI(json);
		
		//控制按钮的状态
		var isPush = json.isPush;
		if(isPush == 1){
			document.getElementById("changeButton").src = "/weixin/v1/img/match_unlock.png";
		}else if(isPush == 0){
			document.getElementById("changeButton").src = "/weixin/v1/img/match_lock.png";
		}
		if(json.device.communicationType==2){
			document.getElementById("wifi_setting").style.display="block";
		}
		
		
	}
	function cutstr(str, len) {
		var str_length = 0;
		var str_len = 0;
		str_cut = new String();
		str_len = str.length;
		for (var i = 0; i < str_len; i++) {
			a = str.charAt(i);

			str_length++;
			if ((/[\u4e00-\u9fa5]+/).test(a)) {
				str_length++;
			}

			str_cut = str_cut.concat(a);

			if (str_length > len) {
				str_cut = str_cut.substr(0, i);
				str_cut = str_cut.concat("...");
				return str_cut;
			}
		}
		//如果给定字符串小于指定长度，则返回源字符串； 
		if (str_length <= len) {
			return str;
		}
	}

	//初始化数据  
	function init_style() {
		var bottom = document.getElementById("bottom");
		var h = document.documentElement.clientHeight - 70 - 70 - 150 - 5;
		bottom.style.height = h + "px";
		if (h < 90) {
			bottom.style.height = "90px";
		}
	}

	//二维码页面
	function showQRCode() {
		window.location.href = "/page/setting/deviceQrcode?deviceId="
				+ document.getElementById("deviceId").value + "&&deviceType="
				+ document.getElementById("deviceType").value + "&&openId="
				+ document.getElementById("openId").value + "&&num="
				+ new Date();
	};
	
	function showWIFISetting(){
		window.location.href = "/weixin/v1/setting/manydevice/airkiss_setting.jsp?openId="
			+ document.getElementById("openId").value + "&&num=1";
	}
	
	function ajaxSendText() {
		var _openId = document.getElementById("openId").value;
		var xhr = createXmlHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var json = xhr.responseText;
					var jsonData = eval("(" + json + ")");
					wx.closeWindow();
				}
			}
		};
		var url = ctx + "/wechat/ajax_airkiss_setting_servlet";
		xhr.open("post", url + "?openId=" + _openId, true);
		xhr.send();
	}
	
	function zhinan() {
		var deviceType = document.getElementById("deviceType").value;
		window.location.href = "/page/setting/deviceIntroduce";
	}

	//设备用户列表
	function showBinding() {
		window.location.href = "/lifesense-healthcenter/wechat/weightbinding_v3?deviceId="
				+ document.getElementById("deviceId").value + "&&openId="
				+ document.getElementById("openId").value + "&&num="
				+ new Date();

	}

	/**
	 * 关闭比赛提示框
	 */
	function deleteDevice(type) {
		document.getElementById("deviceName_del").innerHTML = jsondata.device.deviceName;
		alertWin(type);
	}
	/**
	 *	提示框
	 */
	function alertWin(type) {
		var myAlert = document.getElementById(type);
		document.body.style.display = "block";
		myAlert.style.display = "block";
		var mybg = document.createElement("div");
		mybg.setAttribute("id", "alert_bg");
		mybg.style.background = "#000";
		mybg.style.width = "100%";
		mybg.style.height = document.body.scrollHeight + "px";
		mybg.style.position = "fixed";
		mybg.style.top = "0";
		mybg.style.left = "0";
		mybg.style.zIndex = "500";
		mybg.style.opacity = "0.3";

		document.body.appendChild(mybg);
		document.body.style.overflow = "hidden";
		if (type == 'alert_edit') {
			document.getElementById("deviceName").value = jsondata.device.deviceName;
			document.getElementById("prompt").innerHTML = "";
		}

	}
	/**
	 * 退出框取消按钮 / 比赛说明关闭按钮
	 */
	function alertExitCancel(cur) {
		removeMybg(cur);
		scroll(0, 0);
	}
	/**
	 * 关闭弹出框
	 */
	function removeMybg(cur) {
		var mybg = document.getElementById("alert_bg");
		var myAlert = document.getElementById(cur);
		document.body.style.overflow = "visible";
		myAlert.style.display = "none";

		if (mybg != null) {
			document.body.removeChild(mybg);
		}
		;
	}
	/**
	 * 退出框确认按钮
	 */
	function alertExitOk(cur) {
		if (cur == 'alert_del') {
			var deviceId = document.getElementById("deviceId").value;
			var openId = document.getElementById("openId").value;
			//     alert(1);
			unbindDevice(deviceId.toUpperCase(), 2, openId);
			/* deleteDeviceDetail(); */
			removeMybg(cur);
		} else if (cur == 'alert_edit') {
			//editDevice();
			//removeMybg(cur);
			alertCheck(cur);
		}
	}
	//验证昵称
	function alertCheck(cur) {
		var deviceName = document.getElementById("deviceName");
		var prompt = document.getElementById("prompt");
		//document.getElementById("alert_bottom").style.marginTop="0px";
		if (deviceName.value == "") {
			prompt.innerHTML = "设备名称不能为空。";
			return;
		} else if (deviceName.value == jsondata.device.deviceName
				|| trim(deviceName.value) == jsondata.device.deviceName) {
			/* prompt.innerHTML="设备名称已存在，请重新输入。"; */
			/* return;  */
			removeMybg(cur);
			scroll(0, 0);
		} else if (deviceName.value.trim() == "") {
			deviceName.value = "";
			deviceName.focus();
			return;
		} else {
			//匹配中文 数字 字母 下划线 
			var name = (deviceName.value).replace(/[ ]/g, "");
			var result = checkInput(name);
			if (result == false) {
				editDevice(cur);
			} else {
				prompt.innerHTML = "设备名称只能包含汉字、字母、数字、下划线。";
				//	document.getElementById("alert_bottom").style.marginTop="5px";
				return;
			}
		}
	}

	function checkInput(str) {
		var pattern = /^[\w\u4e00-\u9fa5\-]+$/gi;
		if (pattern.test(str)) {
			return false;
		}
		return true;
	}

	function trim(str) { //删除左右两端的空格  
		return str.replace(/(^\s*)|(\s*$)/g, "");
	}

	//修改昵称
	function editDevice(cur) {
		var prompt = document.getElementById("prompt");
		var deviceId = document.getElementById("deviceId").value;
		var openId = document.getElementById("openId").value;
		var deviceName = trim(document.getElementById("deviceName").value);
		var xhr = createXmlHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var isExist = xhr.responseText;
					if (isExist == 0) {
						ajaxDeviceDetail();
						prompt.innerHTML = "";
						removeMybg(cur);
						scroll(0, 0);
					} else if (isExist == 1) {
						prompt.innerHTML = "设备名称已存在，请重新输入。";
						return;
					} else if (isExist == 2) {
						prompt.innerHTML = "设备名称不能包含表情符号，请重新输入。";
						return;
					}
				}
			}
		};
		var url = ctx + "/device/ajax_edit_device";
		xhr.open("post", url + "?deviceId=" + deviceId + "&&openId=" + openId
				+ "&&deviceName=" + deviceName + "&&action=edit" + "&&clear="
				+ new Date(), true);
		xhr.send();
	}
	function deleteDeviceDetail() {
		var deviceId = document.getElementById("deviceId").value;
		var openId = document.getElementById("openId").value;
		var xhr = createXmlHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var memberId = document.getElementById("memberId").value;
					window.location.href = ctx
							+ "/v3/setting/manydevice/device_list.jsp?memberId="
							+ memberId + "&&openId=" + openId + "&&clear="
							+ new Date();
					// window.location.href = ctx+"/wechat/wxJsApiServlet?memberId="+memberId+"&openId="+openId+"&type=3&clear="+new Date();
				}
			}
		};
		var url = ctx + "/device/ajax_edit_device";
		xhr.open("post", url + "?deviceId=" + deviceId + "&&openId=" + openId
				+ "&&action=deleted" + "&&clear=" + new Date(), true);
		xhr.send();
	}
	
	
	//切换按钮的显示状态
	function clickLockImg(){
		var changeButton = document.getElementById("changeButton");
		var src = changeButton.src;
		
		if(src.indexOf("unlock") >= 0){
			changeButton.src = ctx + "/v3/img/match_lock.png";
			isPush = 0;
		}else{
			changeButton.src = ctx + "/v3/img/match_unlock.png";
			isPush = 1;
		}
		console.log(isPush);
		checkChange++;
	}
	
	var isPush = 0;
	var checkChange = 0;
	window.addEventListener("pagehide", function(){
		updateDeviceIsPush();//Ajax请求 
	    setTimeout(function(){},1000); 
	}, false);
	
	
	
	//修改用户是否接收公共设备链接的通知
	function updateDeviceIsPush(){
		
		var deviceId = document.getElementById("deviceId").value;
		var openId = document.getElementById("openId").value;
		
		var xhr = createXmlHttpRequest();
		if(checkChange == 0){
			return;
		} 
		
		var url = ctx + "/device/device_push_status_servlet";
		xhr.open("post",url + "?openId=" + openId + "&deviceId="+ deviceId + "&isPush=" + isPush + "&clear=" + new Date() * 1, false);
		xhr.send();
		
	}
	
</script>

<script type="text/javascript">
	/**
	 * api初始化
	 */
	function openWXLib() {
		WeixinJSBridge.invoke('openWXDeviceLib', {}, function(res) {
			// console.log(res);
			// alert(res.err_msg);
		});
	}
	/**
	 *	api释放
	 */
	function closeWXLib() {
		WeixinJSBridge.invoke('closeWXDeviceLib', {}, function(res) {
			// console.log(res);
			// alert(res.err_msg);
		});
	}
	/**
	 * 链接设备
	 */
	function connectWXD(deviceId) {
		WeixinJSBridge.invoke('connectWXDevice', {
			"deviceId" : deviceId
		}, function(res) {
			//	 console.log(res.err_msg);
			// alert(res.err_msg);
		});

	}
	/**
	 * 断开设备
	 */
	function disconnectWXD(deviceId) {
		WeixinJSBridge.invoke('disconnectWXDevice', {
			"deviceId" : deviceId
		}, function(res) {
			//	 console.log(res.err_msg);
			// alert(res.err_msg);
		});

	}

	/**
	 * 获取Ticket执行绑定操作
	 */
	function unbindDevice(deviceId, type, openId) {
		closeWXLib();
		openWXLib();
		WeixinJSBridge.invoke('getWXDeviceTicket', {
			"deviceId" : deviceId,
			"type" : type
		}, function(res) {
			// console.log(res);
			var ticketId = res.ticket;
			// alert(ticketId);
			var xhr = createXmlHttpRequest();
			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						var json = xhr.responseText;
						var jsonData = eval("(" + json + ")");//转换后的JSON对象
						if (jsonData == "0") {
							// alert("unbind done");
							// window.location.href=ctx+"/v3/setting/manydevice/device_list.jsp?memberId="+memberId+"&&openId="+openId+"&&clear="+new Date();
							// window.location.href = ctx+"/wechat/wxJsApiServlet?memberId="+memberId+"&openId="+openId+"&type=3&clear="+new Date();
							var memberId = document.getElementById("memberId").value;
							var openId = document.getElementById("openId").value;
							window.location.href = ctx + "/v3/setting/manydevice/device_list.jsp?memberId=" + memberId + "&&openId=" + openId + "&&clear=" + new Date() * 1;
// 							deleteDeviceDetail();
						} else {
							// alert("unbind error");
						}
					}
				}
			};
			var url = ctx + "/wechat/bindDeviceWxServlet"; //初始化
			xhr.open("post", url + "?type=" + type + "&ticket=" + ticketId
					+ "&deviceId=" + deviceId + "&openId=" + openId + "&clear="
					+ new Date() * 1, true);
			xhr.send();
		});
	}
</script>
<script type="text/javascript"
	src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script type="text/javascript">
	var checkJsApi = false;
	wx.config({
		debug : false,
		appId : document.getElementById("hidden-appId").value,
		timestamp : document.getElementById("hidden-timestamp").value,
		nonceStr : document.getElementById("hidden-nonceStr").value,
		signature : document.getElementById("hidden-signature").value,
		jsApiList : [ 'onWXDeviceBindStateChange', 'onWXDeviceStateChange',
				'onScanWXDeviceResult', 'onReceiveDataFromWXDevice',
				'onWXDeviceBluetoothStateChange', 'openWXDeviceLib',
				'closeWXDeviceLib', 'getWXDeviceInfos', 'getWXDeviceTicket',
				'setSendDataDirection', 'startScanWXDevice',
				'stopScanWXDevice', 'connectWXDevice', 'disconnectWXDevice',
				'sendDataToWXDevice', 'scanQRCode',
				'onMenuShareTimeline',
		        'onMenuShareAppMessage',
		        'onMenuShareQQ',
		        'onMenuShareWeibo',
		        'hideMenuItems',
		        'showMenuItems',
		        'hideAllNonBaseMenuItem',
		        'showAllNonBaseMenuItem',
		        'translateVoice',
		        'playMusic',
		        'startRecord',
		        'stopRecord',
		        'onRecordEnd',
		        'playVoice',
		        'pauseVoice',
		        'stopVoice',
		        'uploadVoice',
		        'downloadVoice',
		        'chooseImage',
		        'previewImage',
		        'uploadImage',
		        'downloadImage',
		        'getNetworkType',
		        'openLocation',
		        'getLocation',
		        'hideOptionMenu',
		        'showOptionMenu',
		        'closeWindow',
		        'scanQRCode',
		        'chooseWXPay',
		        'getLatestAddress',
		        'editAddress',
		        'openProductSpecificView',
		        'addCard',
		        'chooseCard',
		        'checkJsApi',
		        'configWXDeviceWiFi'
		        ]
	});
	wx.ready(function() {
		wx.hideOptionMenu();
		
		wx.checkJsApi({
		      jsApiList: [
		        'openWXDeviceLib'
		      ],
		      success: function (res) {
// 		        alert(JSON.stringify(res));
// 		        alert(JSON.stringify(res).checkResult.openWXDeviceLib);
				//判断微信版本是否支持jsspi不支持不显示删除设备按钮
				if(document.getElementById("popt").style.display != "block"){
					if(res.checkResult.openWXDeviceLib){
			        	checkJsApi = true; 
			        	document.getElementById("bottom").style.display = "block";
			      	}else{
			      		checkJsApi = false; 
			      		document.getElementById("bottom").style.display = "none";
			      	}	
				}
		      }
		    });
		
	});
</script>
</html>